package com.project.controller;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.github.pagehelper.PageInfo;
import com.project.bean.BuyInfoBean;
import com.project.bean.RootBean;
import com.project.bean.SellCarBean;
import com.project.bean.UserBean;
import com.project.service.INoteService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    @Autowired
    private  INoteService noteService;

    /**
     * 登录
     * @param code 用户名
     * @param pwd 密码
     * @param request 协议请求
     * @return
     */
    @RequestMapping("login")
    public UserBean login(String code, String pwd, HttpServletRequest request){
        //调用实现类的登陆方法，得到用户对象和他的权限集合
        UserBean user = service.login(code,pwd);
        System.out.println(user);
        if(user == null){
            return null;
        }

        //创建一个用户的session对象
        request.getSession().setAttribute("loginUser",user);

        //得到登陆用户的权限集合
        List<RootBean> rootList = user.getUserRoot();
        //把用户权限的名称放到一个String对象里面
        List<String> rootString = new ArrayList<>();
        //循环添加权限名称到集合中
        for (RootBean r:rootList) {
            rootString.add(r.getName());
        }
        //判断此集合里面是否有对应的权限，并创建对应的购物车session对象
        if(rootString.contains("销售出库")){
            Map<Integer, SellCarBean> sellCarMap = new HashMap<>();
            request.getSession().setAttribute("sellCarMap",sellCarMap);
            System.out.println(sellCarMap);
        }
        if(rootString.contains("采购入库")){
            Map<Integer, BuyInfoBean> buyCarMap = new HashMap<>();
            request.getSession().setAttribute("buyCarMap",buyCarMap);
            System.out.println(buyCarMap);
        }


        return user;
    }

    /**
     * 按条件擦护照用户列表
     * @param pageNO
     * @param name
     * @return
     */
    @RequestMapping("findByItem")
    public PageInfo<UserBean> findByItem(int pageNO, String name){
       return service.findByItem(pageNO,name);
    }

    /**
     * 添加用户
     * @param user 用户对象
     * @param roleIdArray 用户的角色列表
     * @return 添加成功或失败的信息
     */
    @RequestMapping("addUser")
    public String addUser(UserBean user, int[] roleIdArray) {
        return service.addUser(user,roleIdArray);
    }

    /**
     * 删除用户
     * @param id
     */
    @RequestMapping("delUser")
    public String delUser(int id){
        service.delUser(id);
        return "ok";
    }

    /**
     * 按用户id查找用户
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public UserBean findById(int id) {
        return service.findById(id);
    }

    /***
     * 修改用户的权限
     * @param id
     * @param roleIdArray
     */
    @RequestMapping("update")
    public void update(int id, int[] roleIdArray){
        service.update(id,roleIdArray);
    }

    /**
     * 用户修改密码
     * @param newPwd 新密码
     */
    @RequestMapping("updatePwd")
    public String updatePwd(String oldPwd, String newPwd,HttpServletRequest request){
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        if(!user.getPwd().equals(oldPwd)){
            return "no";
        }
        service.updatePwd(user.getId(),newPwd);
        return "ok";
    }

    @RequestMapping("getUser")
    public UserBean getUser(HttpServletRequest request){
        System.out.println((UserBean) request.getSession().getAttribute("loginUser"));
        return (UserBean) request.getSession().getAttribute("loginUser");
    }

}
